package com.shangfutai.android.keymonitoring

import android.app.*
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Color
import android.net.Uri
import android.os.*
import androidx.annotation.RequiresApi

class KeyMonitoringService : Service() {
    companion object{
        fun start(context: Context){
            context.startService(Intent(context,KeyMonitoringService::class.java))
        }
    }

    var CHANNEL_ONE_ID = "CHANNEL_ONE_ID"
    var CHANNEL_ONE_NAME = "CHANNEL_ONE_ID"
    var notificationChannel: NotificationChannel? = null
    @RequiresApi(Build.VERSION_CODES.O)
    override fun onCreate() {
        super.onCreate()


        val intentFilter= IntentFilter()
        intentFilter.addAction("android.intent.action.PTT.up");
        intentFilter.addAction("android.intent.action.PTT.down");
        intentFilter.addAction("android.intent.action.SOS.down");
        intentFilter.addAction("android.intent.action.SOS.up");
        intentFilter.addAction("android.intent.action.CAMERA.up");
        intentFilter.addAction("android.intent.action.CAMERA.down");
        intentFilter.addAction("android.intent.action.FN.down");
        intentFilter.addAction("android.intent.action.FN.up");

        intentFilter.addAction("android.intent.action.SOS.longpress");
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        val mKeyMonitoringReceiver=KeyMonitoringReceiver()
        registerReceiver(mKeyMonitoringReceiver,intentFilter)


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = NotificationChannel(
                CHANNEL_ONE_ID,
                CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_HIGH
            )
            notificationChannel?.enableLights(true)
            notificationChannel?.setLightColor(Color.RED)
            notificationChannel?.setShowBadge(true)
            notificationChannel?.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC)
            val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            manager.createNotificationChannel(notificationChannel!!)
        }
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(""))
        val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
        val notification: Notification = Notification.Builder(this).setChannelId(CHANNEL_ONE_ID)
            .setTicker("键位检测")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("检测键位使用")
            .setContentIntent(pendingIntent)
            .setContentText("检测键位使用")
            .build()
        notification.flags = notification.flags or Notification.FLAG_NO_CLEAR
        startForeground(1, notification)
        
    }

    override fun onBind(intent: Intent): IBinder? {
       return null
    }
}